package com.ljf.framework.util;

import java.util.Arrays;
import java.util.List;

/**
 * 说明：ljf 内部工具类
 *
 * @Auther: lijinfeng
 * @Date: 2024/4/10
 */
public class LjfUtils {
    public static List<String> logLevelList = Arrays.asList("", "trace", "debug", "info", "warn", "error", "fatal");

    /**
     * 将日志等级从 String 格式转化为 int 格式
     * @param level /
     * @return /
     */
    public static int translateLogLevelToInt(String level) {
        int levelInt = logLevelList.indexOf(level);
        if(levelInt <= 0 || levelInt >= logLevelList.size()) {
            levelInt = 1;
        }
        return levelInt;
    }

    /**
     * 将日志等级从 String 格式转化为 int 格式
     * @param level /
     * @return /
     */
    public static String translateLogLevelToString(int level) {
        if(level <= 0 || level >= logLevelList.size()) {
            level = 1;
        }
        return logLevelList.get(level);
    }

    /**
     * 将指定值转化为指定类型
     * @param <T> 泛型
     * @param obj 值
     * @param cs 类型
     * @return 转换后的值
     */
    @SuppressWarnings("unchecked")
    public static <T> T getValueByType(Object obj, Class<T> cs) {
        // 如果 obj 为 null 或者本来就是 cs 类型
        if(obj == null || obj.getClass().equals(cs)) {
            return (T)obj;
        }
        // 开始转换
        String obj2 = String.valueOf(obj);
        Object obj3 = null;
        if (cs.equals(String.class)) {
            obj3 = obj2;
        } else if (cs.equals(int.class) || cs.equals(Integer.class)) {
            obj3 = Integer.valueOf(obj2);
        } else if (cs.equals(long.class) || cs.equals(Long.class)) {
            obj3 = Long.valueOf(obj2);
        } else if (cs.equals(short.class) || cs.equals(Short.class)) {
            obj3 = Short.valueOf(obj2);
        } else if (cs.equals(byte.class) || cs.equals(Byte.class)) {
            obj3 = Byte.valueOf(obj2);
        } else if (cs.equals(float.class) || cs.equals(Float.class)) {
            obj3 = Float.valueOf(obj2);
        } else if (cs.equals(double.class) || cs.equals(Double.class)) {
            obj3 = Double.valueOf(obj2);
        } else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) {
            obj3 = Boolean.valueOf(obj2);
        } else if (cs.equals(char.class) || cs.equals(Character.class)) {
            obj3 = obj2.charAt(0);
        } else {
            obj3 = (T)obj;
        }
        return (T)obj3;
    }

    /**
     * 判断是否windows系统
     * @return
     */
    public static boolean isRunInWindows(){
        return System.getProperty("os.name").toLowerCase().startsWith("win");
    }

    /**
     * 判断否是linux系统
     * @return
     */
    public static boolean isRunInLinux(){
        return System.getProperty("os.name").toLowerCase().startsWith("linux");
    }

    /**
     * 判断是否运行在IDEA
     * @return
     */
    public static boolean isRunInIDEA() {
        try {
            Class.forName("com.intellij.rt.execution.application.AppMainV2");
            return true;
        } catch (ClassNotFoundException ignored) {
            return false;
        }
    }

}
